<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    request.setCharacterEncoding("UTF-8");
    String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>公司管理</title>
    <jsp:include page="../../../head.jsp"/>
    <script type="text/javascript">

        function ajaxDataFilter(treeId, parentNode, responseData) {
            if (!responseData.depts)return null;
            return responseData.depts;
        }
        ;

        function ajaxDataFilter2(treeId, parentNode, responseData) {
            if (!responseData.users)return null;
            return responseData.users;
        }
        ;

        function zTreeOnCheck(event, treeId, treeNode) {
            var path = "";
            var checked = treeNode.checked;
            if (checked) {
                if (treeNode.isParent) {
                    path = "../base/inform-getUserByCompanyId?company.id=" + treeNode.id;
                } else {
                    path = "../base/inform-getUserByDeptId?dept.id=" + treeNode.id;
                }
                var setting2 = {
                    async: {
                        enable: true,
                        url: path,
                        type: "post",
                        dataType: "json",
                        dataFilter: ajaxDataFilter2
                    },
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        chkboxType: {"Y": "ps", "N": "ps"}
                    },
                    view: {
                        showIcon: false
                    },
                    callback: {
                        onCheck: function (event, treeId, treeNode) {
                            if (treeNode.checked) {
                                var sname = $("#sname").html();
                                var userids = $("#userids").val();
                                if (userids == "" || userids == null) {
                                    $("#userids").val(treeNode.id);
                                } else {
                                    $("#userids").val(userids + "," + treeNode.id);
                                }
                                if (sname == "" || sname == null || sname == "已选") {
                                    $("#sname").html(sname + "<br/>" + treeNode.name);
                                } else {
                                    $("#sname").html(sname + "," + treeNode.name);
                                }
                            } else {
                                alert("3");
                            }
                        }
                    }
                };
                $.fn.zTree.init($("#prePerson"), setting2);
            } else {
                //清除预算人员名单
            }
        }
        ;

        var setting = {
            async: {
                enable: true,
                url: "company-listDept",
                type: "post",
                dataType: "json",
                autoParam: ["id=company.id", "name"],
                dataFilter: ajaxDataFilter
            },
            check: {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: {"Y": "ps", "N": "ps"}
            },
            callback: {
                onCheck: zTreeOnCheck
            }
        };

        $(function () {
            initTree("inform-listCompany", null);
        });

        /**初始化公司树*/
        function initTree(src, params) {
            $.post(src, params, function (value) {
                if (!value.companys)return;
                $.fn.zTree.init($("#tree"), setting, value.companys);
            });
        }

        $(function () {
            $("#mrdForm").mrdForm({
                title: "查询列表",// 表单标题
                columns: [{
                    name: "name",
                    label: "公司名称"
                }],
                buttons: {
                    'custom': [
                        //{id:"addDept",value:"添加",click:add},
                        //{id:"modify",value:"编辑",click:edit}
                    ],
                    queryEvent: function () {
                        var rules = "";// 定义rules
                        $.each($("#" + $.mrdForm.formContainerId
                                + " td[id^='td_control_']"), function (i, n) {
                            var control = $(n).children().first();
                            if (!$.mrdForm.isEmpty(control.val())) {
                                rules += "{'field':'" + control.attr('name')
                                        + "','op':'cn','data':'"
                                        + control.val() + "'},";// 组装rules
                            }
                        });
                        // 如果没有查询条件，则返回
                        if (!$.mrdForm.isEmpty(rules)) {
                            rules = rules.substring(0, rules.length);
                            var filters = '{"groupOp":"AND","rules":[' + rules + ']}';// 定义filters
                            initTree("company-listCompany", {filters: filters});
                        } else {
                            initTree("company-listCompany", null);
                        }
                    }
                }
            });
            $("#mrdForm").mrdForm("resize");

            //加载发送类型
            $.post("sysval-gridData", {pid: "B"}, function (data) {
                var options = "";
                if (data.systemValues != null)
                    $(data.systemValues).each(function (i) {
                        options = options + "<option value='" + this.id + "'>" + this.chn + "</option>";
                    });
                $("#sendtype").empty();
                $("#sendtype").append(options);
            });
        });


        $(function () {
            //初始化表单验证引擎
            $("#inform").validationEngine("attach", {
                //消息位置
                promptPosition: "bottomRight",
                scroll: false
            });
            //初始化ajaxForm参数
            var options = {
                //提交方式
                type: "POST",
                //提交数据格式
                dataType: "json",
                //在提交之前做的操作
                beforeSubmit: function () {
                    return $("#inform").validationEngine("validate");
                },
                //成功后做的操作
                success: function (responseText, statusText, xhr, $form) {
                    alert(responseText.msg);
                    location.reload(true);
                }
            };
            $('#inform').ajaxForm(options);
        });
        function submitTheForm() {
            $("#inform").submit();
        }

    </script>
</head>
<body>
<%=htmlData%>
<div id="mrdForm" style="display: block;"></div>
<div style="display: block;">
    <fieldset style="width: 300px;height:300px;float: left;">
        <legend>公司部门列表</legend>
        <ul id="tree" class="ztree" style="width:230px; overflow:auto;"></ul>
    </fieldset>
    <fieldset style="width: 150px;height:300px;float: left;">
        <legend>待选人员列表</legend>
        <ul id="prePerson" class="ztree" style="width:230px; overflow:auto;"></ul>
    </fieldset>
    <div style="float:left;">
        <fieldset>
            <legend>已选人员列表</legend>
            <div id="sname" style="width: 261px;height:150px;border:1px groove;">已选</div>
            <div style="clear:both;">
                <form id="inform" action="inform-save" method="post">
                    <div>
                        <input type="hidden" id="userids" name="userids"/>
                        <textarea style="margin:0px;" rows="3" cols="30" id="contents" name="inform.contents"
                                  class="validate[required]">发送内容：</textarea>*
                    </div>
                    <div>发送方式：<select id="sendtype" name="inform.types"></select></div>
                    <div style="float: right"><input type="button" value="发送" onclick="submitTheForm();"/></div>
                </form>
            </div>
        </fieldset>
    </div>
</div>

</body>
</html>
